<?php
namespace app\api\controller;

use app\api\BaseController;
use app\api\service\BannerService;
use app\api\service\GoodsCategoryService;
use app\api\service\GoodsService;
use app\api\service\GroupActivityService;
use app\api\service\GroupGoodsService;
use app\api\service\SpikeGoodsService;
use think\facade\Db;
use think\facade\Filesystem;

class IndexController extends BaseController
{

      /**
     * 上传接口
     * */
    public function upload()
    {
        $file = $this->request->file('file');

        if (!$file->isFile()){
            return  json(['code' => 0, 'msg' => '无效的文件!']);
        }

        $savename = Filesystem::disk('public')->putFile('api', $file);

        return json(['code' => 1, 'src' => $this->request->domain() . '/storage/' .$savename,'msg' => '上传成功!']);
    }

    /**
     * app版本
     */
    public function appVersion()
    {
        $param = $this->request->param();
        $store_id = empty($param['store_id'])?1:$param['store_id'];
        $version = Db::name('config')->where('store_id', $store_id)->where('key', 'version')->value('value');
        $app_path = Db::name('config')->where('store_id', $store_id)->where('key', 'app_path')->value('value');
        $app_path_wgt = Db::name('config')->where('store_id', $store_id)->where('key', 'app_path_wgt')->value('value');
        $app_up_tips = Db::name('config')->where('store_id', $store_id)->where('key', 'app_up_tips')->value('value');
        if ($app_up_tips) {
            $app_up_tips = explode(';', $app_up_tips);
        } else {
            $app_up_tips = [];
        }
        return json(['code' => 1, 'msg' => '查询成功', 'data' => ['app_path'=>$app_path,'app_up_tips'=>$app_up_tips,'version'=>$version, 'app_path_wgt'=>$app_path_wgt]]);
    }

    public function systemConfig()
    {
        $param = $this->request->param();
        $store_id = empty($param['store_id'])?1:$param['store_id'];
        $rst = Db::name('config')->where('store_id', $store_id)->where('key', $param['key'])->value('value');
        return json(['code' => 1, 'msg' => '查询成功', 'data' => $rst]);
    }

    public function index()
    {

        /**
         * 获取首页分类
         * */
        $store_id = $this->request->param('store_id/d');

        if (empty($store_id)){
            return  json(['code' => 0,'非法参数!']);
        }

        /**
         * 获取首页一级分类
         * */
        $categories = GoodsCategoryService::getHomeCategoryList($store_id);



        /**
         * 获取首页轮播
         * */
        $banners = BannerService::getBannerList($store_id);




        /**
         * 获取首页商品
         * */
        $goodsList = GoodsService::getHomeGoodsList($store_id);



        /**
         * 获取首页活动图片
         * */
        $activityCoverList = GroupActivityService::getHomeGroupActivityImage($store_id);



        /**
         * 获取秒杀商品
         * */
        $spikeGoodsList = SpikeGoodsService::getHomeSpikeGoodsList($store_id);


        /**
         * 获取拼团商品
         * */
        $groupGoodsList = GroupGoodsService::getHomeGroupGoods($store_id);



        /**
         * 获取会员专享商品
         * */
        $memberGoodsList = GoodsService::getMemberGoodsList($store_id);


        /**
         * 获取补贴商品
         * */
        $subsidyGoodsList = GoodsService::getSubsidyGoodsList($store_id);

        $data  = [
            'categories' => $categories,
            'banners' => $banners,
            'activityCoverList' =>$activityCoverList,
            'goodsList' => $goodsList,
            'spikeGoodsList' => $spikeGoodsList,
            'groupGoodsList' => $groupGoodsList,
            'memberGoodsList' => $memberGoodsList,
            'subsidyGoodsList' => $subsidyGoodsList
        ];



        return json(['code' => 1, 'data' => $data,'msg' => '请求成功!']);
    }

}
